﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;

namespace CriptoFile
{
    public class Crypto
    {
        private byte[] key;
        private byte[] iv;

        public bool Is128bit
        {
            get;
            set;
        }

        public bool Is256bit
        {
            get;
            set;
        }

       

        public Crypto()
        {
            Is128bit = true;
            Is256bit = false;
        }

        public void CreateKey(String key, bool in128bit)
        {
            Byte[] b = ASCIIEncoding.ASCII.GetBytes(key);
            System.Security.Cryptography.MD5 sscMD5 = System.Security.Cryptography.MD5.Create();
            // recuperiamo i bytes dell'hash
            this.key = sscMD5.ComputeHash(b);
            if (!Is128bit)
            {
                int len = this.key.Length;
                Byte[] rev = this.key;
                Array.Reverse(rev);
                Array.Resize<byte>(ref this.key, len * 2);
                Array.Copy(rev, 0, this.key, len, len);
          
            }
            iv = this.key;
            Array.Reverse(iv);
       
        }

        public String GetKey()
        {
            return ASCIIEncoding.ASCII.GetString(key);
        }

        public byte[] EncodeByte(byte[] palainbyte)
        {
            RijndaelManaged rjm = new RijndaelManaged();
            if (Is128bit)
            {
                rjm.KeySize = 128;
                rjm.BlockSize = 128;
            }
            else
            {
                rjm.KeySize = 256;
                rjm.BlockSize = 256;
            }
            rjm.Key = key;//ASCIIEncoding.ASCII.GetBytes(key);
            rjm.IV = iv;//ASCIIEncoding.ASCII.GetBytes(iv);
            return rjm.CreateEncryptor().TransformFinalBlock(palainbyte,0,palainbyte.Length);
        }

        //public string Encode(string S)
        //{
        //    RijndaelManaged rjm = new RijndaelManaged();
        //    rjm.KeySize = 128;
        //    rjm.BlockSize = 128;
        //    rjm.Key = ASCIIEncoding.ASCII.GetBytes(key);
        //    rjm.IV = ASCIIEncoding.ASCII.GetBytes(iv);
        //    Byte[] input = Encoding.UTF8.GetBytes(S);
        //    Byte[] output = rjm.CreateEncryptor().TransformFinalBlock(input, 0, input.Length);
        //    return Convert.ToBase64String(output);
        //}


        public Byte[] DecodeByte(byte[] encryptbytes)
        {
            RijndaelManaged rjm = new RijndaelManaged();
            if (Is128bit)
            {
                rjm.KeySize = 128;
                rjm.BlockSize = 128;
            }
            else
            {
                rjm.KeySize = 256;
                rjm.BlockSize = 256;
            }
            rjm.Key = key;//ASCIIEncoding.ASCII.GetBytes(key);
            rjm.IV = iv;// ASCIIEncoding.ASCII.GetBytes(iv);
            try
            {
               
                Byte[] output = rjm.CreateDecryptor().TransformFinalBlock(encryptbytes, 0, encryptbytes.Length);
                return output;
            }
            catch
            {
                return new Byte[0];
            }
        }


        //public string Decode(string S)
        //{
        //    RijndaelManaged rjm = new RijndaelManaged();
        //    rjm.KeySize = 128;
        //    rjm.BlockSize = 128;
        //    rjm.Key = ASCIIEncoding.ASCII.GetBytes(key);
        //    rjm.IV = ASCIIEncoding.ASCII.GetBytes(iv);
        //    try
        //    {
        //        Byte[] input = Convert.FromBase64String(S);
        //        Byte[] output = rjm.CreateDecryptor().TransformFinalBlock(input, 0, input.Length);
        //        return Encoding.UTF8.GetString(output);
        //    }
        //    catch
        //    {
        //        return S;
        //    }
        //}
    }




}
